#include "GUI.h"	// for Score_window only (doesn't really belong in Window.h)
#include "Game.h"

using namespace Graph_lib;

class Game;

// Score_window is basic scaffolding for ultra-simple interaction with graphics
// it provides one window with one "next" button for ultra-simple animation

struct Score_window : Window {
	Score_window(Point xy, int w, int h, const string& title )
	: Window(xy,w,h,title),
	  button_pushed(false),
	  next_button(Point(0,y_max() - 100), w, 100, "Quit", cb_next)
	{
		out[0] = new Text(Point(25,100), "no scores");
		out[1] = new Text(Point(25,150), "no scores");
		out[2] = new Text(Point(25,200), "no scores");
		out[3] = new Text(Point(25,250), "no scores");
		out[4] = new Text(Point(25,300), "no scores");

		attach(next_button);

		vector<pair<string, int>> scores = Game::get_instance()->read_score_file("scores.txt");

		for(int i = 0; i < scores.size() && i < 5; ++i)
		{
			string output_string = "";
			string name = scores[i].first;
			char * score = new char[4];
			memset(score, 0, 4);

			itoa(scores[i].second, score, 10);

			output_string = name + ": " + score;
			//output_string += score;

			attach(*out[i]);
			(*out[i]).set_label(output_string);
		}
	}
	
	void wait_for_button()
	// modified event loop
	// handle all events (as per default), but quit when button_pushed becomes true
	// this allows graphics without control inversion
	{
		while (!button_pushed) Fl::wait();
		button_pushed = false;
		Fl::redraw();
	}

	Button next_button;
	//Out_box* out;
	Text* out[5];
private:
	bool button_pushed;
	
	static void cb_next(Address, Address addr) // callback for next_button
	//	{ reference_to<Score_window>(addr).next(); }
	{
		static_cast<Score_window*>(addr)->next();
	}

	void next() { button_pushed = true; }

};
